import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import '../../helper/Helper.dart';
import '../../helper/uidata.dart';
import '../../model/swxg.dart';
import '../../model/xhnl.dart';

import '../../helper/channelHelper.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';

class ResSwKrPage extends StatefulWidget {
  @override
  ResSwKrPage();

  @override
  ResSwKrPageState createState() => new ResSwKrPageState();
}

class ResSwKrPageState extends State<ResSwKrPage> {
  final TextEditingController c1 = new TextEditingController();
  late SWXG x1;
  bool ifOK = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    if (UIData.SkData1 != null) {
      c1.text = Helper.RoundDouble(UIData.SkData1!.H1, 2, "");
    } else {
      c1.text = "850";
    }

    setQ();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(title: Text("水位相关计算")),
      // floatingActionButton: getFloatingButton(context),
      body: ResSwKrBody(context),
    );
  }

  void setQ() async {
    try {
      ifOK = false;

      x1 = await SWXG.Get(c1.text) as SWXG;
      ifOK = true;
      this.setState(() => {});
      ChannelHelper.showToast("计算完成！");
    } catch (e) {
      ChannelHelper.showToast("计算错误！" + e.toString());
    }
  }

  String getStr(double xx) {
    return Helper.RoundDouble(xx, 1, "");
  }

  String getStr2(double xx) {
    return Helper.RoundDouble(xx, 2, " m");
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];

    // list1.add(InputWidget.inCard(ListTile(
    //   leading: IconWidget.ShipImage(),
    //   title: Text("水位相关计算"),
    // )));

    //  list2.add(InputWidget.getButton("计算", this.setQ));
    list2.add(InputWidget.getButton3("水位", c1, this.setQ));

    list1.add(InputWidget.inCard2(list2));
    if (!ifOK) return list1;
    list3.add(InputWidget.getInfo("库容", getStr(x1.V), subtitle: "万m3"));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInfo("可调水量", Helper.RoundDouble(x1.W, 1, ""),
        subtitle: "万m3"));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInfo(
        "单机最大出力", Helper.RoundDouble(x1.MaxP / 4, 1, ""),
        subtitle: "万kW"));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInfo("全站最大出力", Helper.RoundDouble(x1.MaxP, 1, ""),
        subtitle: "万kW"));
    list3.add(InputWidget.getDivider());

    list3.add(InputWidget.getInfo("耗水率", Helper.RoundDouble(x1.HSL, 1, ""),
        subtitle: "m3/kW.h"));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInfo("蓄能值", Helper.RoundDouble(x1.XNZ, 1, ""),
        subtitle: "万kW.h"));

    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInfo(
        "蓄能比", Helper.RoundDouble(x1.XNZ / 19748.28, 2, "")));

    if (ifOK) {
      list1.add(InputWidget.inCard2(list3));
    }

    return list1;
  }

  ResSwKrBody(BuildContext context) => SingleChildScrollView(
        child: ResSwKrFields(context),
      );

  ResSwKrFields(BuildContext context) {
    return Container(child: Column(children: getWidgetList(context)));
  }
}
